Ruby on Rails - scoping routes in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Rails provides several ways to organize our routes.
Scope by URL:
scope 'admin' do
get 'dashboard', to: 'administration#dashboard'
resources 'employees'
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
This generates the following routes
get '/admin/dashboard', to: 'administration#dashboard'
post '/admin/employees', to: 'employees#create'
get '/admin/employees/new', to: 'employees#new'
get '/admin/employees/:id/edit', to: 'employees#edit'
get '/admin/employees/:id', to: 'employees#show'
patch/put '/admin/employees/:id', to: 'employees#update'
delete '/admin/employees/:id', to: 'employees#destroy'
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
It may make more sense, on the server side, to keep some views in a different subfolder, to separate admin views from user views.
Scope by module
scope module: :admin do
get 'dashboard', to: 'administration#dashboard'
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
module looks for the controller files under the subfolder of the given name
get '/dashboard', to: 'admin/administration#dashboard'
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- We can rename the path helpers prefix by adding an "as" paramete
scope 'admin', as: :administration do
get 'dashboard'
end
# => administration_dashboard_path
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Rails provides a convenient way to do all the above, using the
method. - The following declarations are equivalent
namespace :admin do
end
scope 'admin', module: :admin, as: :admin
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Scope by controller
scope controller: :management do
get 'dashboard'
get 'performance'
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Generate these routes
get '/dashboard', to: 'management#dashboard'
get '/performance', to: 'management#performance'
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Shallow Nesting
Resource routes accept a : shallow option that helps to shorten URLs where possible. Resources shouldn't be nested more than one level deep. One way to avoid this is by creating shallow routes. The goal is to leave off parent collection URL segments where they are not needed. The end result is that the only nested routes generated are for the :index ,:create , and :new actions. The rest are kept in their own shallow URL context. There are two options for scope to custom shallow routes:
- :shallow_path: Prefixes member paths with a specified parameter.
scope shallow_path: "sekret" do
resources :articles do
resources :comments, shallow: true
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- :shallow_prefix: Add specified parameters to named helpers
scope shallow_prefix: "sekret" do
resources :articles do
resources :comments, shallow: true
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- You can also illustrate shallow routes more by:
resources :auctions, shallow: true do
resources :bids do
resources :comments
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- alternatively coded as follows (if we are block-happy):
resources :auctions do
shallow do
resources :bids do
resources :comments
end
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Resulting routes are
Prefix | Verb | URI Pattern |
---|---|---|
bid_comments | GET | /bids/:bid_id/comments(.:format) |
POST | /bids/:bid_id/comments(.:format) | |
new_bid_comment | GET | /bids/:bid_id/comments/new(.:format) |
edit_comment | GET | /comments/:id/edit(.:format) |
comment | GET | /comments/:id(.:format) |
PATCH | /comments/:id(.:format) | |
PUT | /comments/:id(.:format) | |
DELETE | /comments/:id(.:format) | |
auction_bids | GET | /auctions/:auction_id/bids(.:format) |
POST | /auctions/:auction_id/bids(.:format) | |
new_auction_bid | GET | /auctions/:auction_id/bids/new(.:format) |
edit_bid | GET | /bids/:id/edit(.:format) |
bid | GET | /bids/:id(.:format) |
PATCH | /bids/:id(.:format) | |
PUT | /bids/:id(.:format) | |
DELETE | /bids/:id(.:format) | |
auctions | GET | /auctions(.:format) |
POST | /auctions(.:format) | |
new_auction | GET | /auctions/new(.:format) |
edit_auction | GET | /auctions/:id/edit(.:format) |
auction | GET | /auctions/:id(.:format) |
PATCH | /auctions/:id(.:format) | |
PUT | /auctions/:id(.:format) | |
DELETE | /auctions/:id(.:format) |
- We analyze the routes generated carefully, We will notice that the nested parts of the URL are only included when they are needed to determine what data to display.